
import 'package:basic_common/app_theme.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_common/time_tool.dart';
import 'package:basic_widgets/app_bar_widget.dart';
import 'package:basic_widgets/image_widget.dart';
import 'package:basic_widgets/layout_widget.dart';
import 'package:basic_widgets/text_widget.dart';

import '../modle/patient_msg_list_data.dart';
import 'msglist_controller.dart';

class MsgListPage extends GetView<MsgListController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: backAppbar(title: "患者消息"),
      body: GetBuilder<MsgListController>(
        id: 'msgList',
        builder: (controller) => controller.patientList.value.length==0?Align(
          alignment: Alignment.topCenter,
          child: EmptyBox(title:"暂无消息")): Container(
          color: Colors.white,
          height: ScreenHelper.screenHeight,
          width: ScreenHelper.screenWidth,
          child: refreshBox
          (
            onLoading: () => controller.onLoading(),
            onRefresh: () => controller.onRefresh(),
            refController: controller.refreshController,
            child: ListView.separated(
                itemBuilder: (context, index) {
                  PatientMsgData? msgData = controller.patientList.value[index];
                  String date = msgData!.createTime!;
                  String newDateStr =TimeTool.strToDate(date);
                      // formatDate(DateTime.parse(date), [yyyy, '-', mm, '-', dd]);
                  return GestureDetector(
                    onTap: () {
                      Get.toNamed(AppRoutes.chat,
                          arguments: {"patientNo": msgData.patientNo});
                    },
                    child: buildMsgItem(
                        title: msgData!.patientname!,
                        head: '',
                        lastTime: newDateStr,
                        subInfo: msgData.msgContent!),
                  );
                },
                separatorBuilder: (context, index) => divider(),
                itemCount: controller.patientList.value.length),
          ),
        ),
      ),
    );
  }

  Widget buildMsgItem(
      {required String title,
      required String head,
      required String lastTime,
      required String subInfo}) {
    return Container(
      color: Colors.white,
      child: ListTile(
        leading: circleImg(head: head),
        title: Row(
          children: [
            Text(title,style: TextStyle(color: AppTheme.dartTextColor),),
            Expanded(
                child: Text(
              lastTime,
              textAlign: TextAlign.end,
              style: TextStyle(
                  color: AppTheme.dartTextColor, fontSize: ScreenHelper.sp(14)),
            ))
          ],
        ),
        subtitle: Text(subInfo.startsWith("http")||subInfo.startsWith("https")?"[图片]":subInfo,style:TextStyle(fontSize: ScreenHelper.sp(14))),
      ),
    );
  }
}
